class_name HCharacterInputNode
extends Node


@export var RotYNode:Node3D
@export var RotXNode:Node3D
@export var RigidNode:HRigidNode

var _forward:bool = false
var _back:bool = false
var _left:bool = false
var _right:bool = false


var mouse_left_pressed:bool = false
var mouse_left_pressed_time:int = 0
var mouse_left_click:bool = false
var _mouse_right:bool = false
var _mouse_move_delta:Vector2 = Vector2.ZERO

# 交互键相关变量
@export var interact_key_code:int = KEY_E # 支持自定义，默认E键
var interact_pressed:bool = false
var interact_pressed_time:int = 0
var interact_click:bool = false

# 鼠标滑轮相关变量
var mouse_wheel_delta:float = 0.0  # 鼠标滑轮滚动值

var _focus_mode:bool = false

func set_focus_mode(p_mode):
	_focus_mode = p_mode
	if p_mode:
		Input.mouse_mode = Input.MouseMode.MOUSE_MODE_CAPTURED
	else:
		Input.mouse_mode = Input.MouseMode.MOUSE_MODE_CONFINED

func clear_mouse_input():
	_mouse_move_delta = Vector2.ZERO
	mouse_left_click = false
	interact_click = false
	mouse_wheel_delta = 0.0  # 清除滑轮滚动值

func update(delta):
	RigidNode.jump = false
	RotYNode.rotate_y(-_mouse_move_delta.x * 0.001)
	RotXNode.rotate_x(-_mouse_move_delta.y * 0.0005)
	var move_vec2:Vector2 = get_move_vec()
	var move_vec3:Vector3 = Vector3(move_vec2.y,0,-move_vec2.x)
	move_vec3 = move_vec3.rotated(Vector3.UP,RotYNode.rotation.y)
	RigidNode.set_move_vec(move_vec3*3)
	var c1 = $"../Rot/RayCast3D2".get_collider()
	var c2 = $"../RayCast3D".get_collider()
	if c1 and c2 and RigidNode.is_on_floor:
		var p1 = $"../Rot/RayCast3D2".get_collision_point()
		var p2 = $"../RayCast3D".get_collision_point()
		var h = p1.y-p2.y
		if h>0.15 and h<0.25:
			RigidNode.set_offset(h+0.01)
	



func get_move_vec():
	_forward = Input.is_key_label_pressed(KEY_W)
	_back = Input.is_key_label_pressed(KEY_S)
	_left = Input.is_key_label_pressed(KEY_A)
	_right = Input.is_key_label_pressed(KEY_D)
	var ret:Vector2
	ret.x = int(_forward) - int(_back)
	ret.y = int(_right) - int(_left)
	return ret


func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouse:
		_deal_mouse_input(event)
	elif event is InputEventKey:
		_deal_interact_key_input(event)


# 统一的按下时长判断函数
func _handle_press_release(is_pressed: bool, pressed_ref: bool, press_time_ref: int, click_ref_setter: Callable) -> Array:
	var new_pressed = pressed_ref
	var new_press_time = press_time_ref
	
	if is_pressed:
		new_pressed = true
		new_press_time = Time.get_ticks_msec()
	else:
		if new_pressed:
			var release_time = Time.get_ticks_msec()
			var press_duration = release_time - new_press_time
			if press_duration < 500:
				click_ref_setter.call(true)
		new_pressed = false
	
	return [new_pressed, new_press_time]

func _deal_mouse_input(p_event:InputEventMouse):
	if _focus_mode == false:
		return
	if p_event is InputEventMouseMotion:
		_mouse_move_delta += p_event.screen_relative
	elif p_event is InputEventMouseButton:
		if p_event.button_index == MOUSE_BUTTON_LEFT:
			var result = _handle_press_release(
				p_event.pressed, 
				mouse_left_pressed, 
				mouse_left_pressed_time,
				func(value): mouse_left_click = value
			)
			mouse_left_pressed = result[0]
			mouse_left_pressed_time = result[1]
		elif p_event.button_index == MOUSE_BUTTON_WHEEL_UP:
			mouse_wheel_delta = 1.0  # 向上滚动
		elif p_event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
			mouse_wheel_delta = -1.0  # 向下滚动

func _deal_interact_key_input(event: InputEventKey):
	if _focus_mode == false:
		return
	if event.keycode == interact_key_code:
		if not event.echo:  # 避免重复按键
			var result = _handle_press_release(
				event.pressed,
				interact_pressed,
				interact_pressed_time,
				func(value): interact_click = value
			)
			interact_pressed = result[0]
			interact_pressed_time = result[1]
